import { createSlice, PayloadAction } from '@reduxjs/toolkit';

interface Post {
    id: number;
    title: string;
}

type PostsState = Post[];

const initialState: PostsState = [];

const postsSlice = createSlice({
    name: 'posts',
    initialState,
    reducers: {
        addPost(state, action: PayloadAction<Post>) {
            state.push(action.payload);
        },
        removePost(state, action: PayloadAction<number>) {
            return state.filter(post => post.id !== action.payload);
        },
    },
});

export const { addPost, removePost } = postsSlice.actions;
export default postsSlice.reducer;
